# -*- encoding: utf-8 -*-
"""
    package.module
    ~~~~~~~~~~~~~~

    程序主目录


"""

from flask import Flask, request, make_response
from flask_sqlalchemy import SQLAlchemy
from config import config
from app.main.apis.base.heath import api_base
db = SQLAlchemy()


def create_app(config_name):
    # laoding config
    app = Flask(__name__)
    app.config.from_object(config[config_name])
    config[config_name].init_app(app)

    # init app
    db.init_app(app)

    # register route and define error page.
    # from main.views import index_page

    # register API


    # base API

 
    # app.register_blueprint(index_page)
   
    app.register_blueprint(api_base, url_prefix='/api')

    # 请求返回给客户端之前
    # 允许跨域请求
    @app.after_request
    def after_request(resp):
        response = make_response(resp)
        response.headers['Access-Control-Allow-Origin'] = '*'
        response.headers['Access-Control-Allow-Methods'] = 'OPTIONS,HEAD,GET,POST'
        response.headers['Access-Control-Allow-Headers'] = 'x-requested-with'
        return response

    # 请求调用之前
    # 验证URL合法性
    @app.before_request
    def validate_request():
        """
        验证用户请求URL 权限 方法
        :return:
        """

        pass

    return app
